package com.foosun.core.controller;

import com.foosun.core.util.filecore.ProgressEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Nelin on 14-2-25.
 */

@Controller
@RequestMapping("/commons/progress")
public class ProgressController {

    public static String PERCENT_NAME = "percentName";

    public static void setProgress(HttpSession session, Object v) throws InterruptedException {
        Object o = session.getAttribute(PERCENT_NAME);
        if (o != null) {
            String percentName = o.toString();
            session.setAttribute(percentName, v);
        }
        //Thread.currentThread().sleep(1);
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public ProgressEntity doPost(String percentName, HttpSession session) throws ServletException, IOException {
        ProgressEntity ps = (ProgressEntity) session.getAttribute(ProgressEntity.PS_NAME);
        return ps == null ? new ProgressEntity():ps;
    }
// @RequestMapping(method = RequestMethod.POST)
//    @ResponseBody
//    public Object doPost(String percentName, HttpSession session) throws ServletException, IOException {
//        Object value = session.getAttribute(percentName);
//        return value ==   null ? 1:value;
//    }

    @RequestMapping("/setting")
    @ResponseBody
    public Boolean setting(String percentName, Boolean reset, HttpSession session) throws ServletException, IOException {
        session.setAttribute(PERCENT_NAME, percentName);
        if (reset) {
            session.removeAttribute(percentName);
        }
        return true;
    }

}
